export function debounce(fn, delay) {       //防抖函数
    var timeout;
    return function () {
        clearTimeout(timeout)
        timeout = setTimeout(() => {
            fn.apply(this, arguments)    //注意传参
        }, delay)
    }
}

export function flatten(arr) {              //数组扁平化
    return arr.toString().split(',').map(function (item) {
        return +item //+可以快速获得Number类型
    })
}

export function getQueryVariable(variable) {    //获取url参数
    var query =
        window.location.search.substring(1) ||
        window.location.href.split("?")[1] ||
        window.location.href;

    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] === variable) {
            return pair[1];
        }
    }
    return false;
}

export function dataURLtoFile(dataurl, filename) { //将base64转换为文件
    var arr = dataurl.split(','),
        mime = arr[0].match(/:(.*?);/)[1],
        bstr = atob(arr[1]),
        n = bstr.length,
        u8arr = new Uint8Array(n);
    while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
    }
    return new File([u8arr], filename, {
        type: mime
    });
}

export function fileTypeToType(name) {  //通过name获取type简化归类
    let index = name.lastIndexOf('.') + 1
    let len = name.length
    let type = name.slice(index, len)
    type = type.toLowerCase()
    let fileType = ""
    let obj = {
        "png": "img",
        "jpg": "img",
        "jpeg": "img",
        "doc": "doc",
        "docx": "doc",
        "xls": "xls",
        "xlsx": "xls",
        "zip": "zip",
        "7z": "zip",
        "rar": "zip",
        "jar": "zip",
        "kz": "zip",
        "mp3": "music",
        "cd": "music",
        "wave": "music",
        "aiff": "music",
        "mpeg": "music",
        "mp4": "video",
        "avi": "video",
        "rmvb": "video",
        "rm": "video",
        "wmv": "video",
        "asf": "video",
        "asx": "video",
        "txt": "txt",
        "html": "html",
        "ppt": "ppt",
        "pdf": "pdf",
    }
    if (!obj[type]) {
        fileType = "otr"
    } else {
        fileType = obj[type]
    }


    return fileType
}

export function fileSizeToSize(size) {  //字节大小转换文字
    let str = '0', unit = 'kb'
    if (size / 1024 / 1024 >= 1) {
        unit = 'mb'
        str = (size / 1024 / 1024).toFixed(1) + unit
    } else {
        unit = 'kb'
        str = (size / 1024).toFixed(1) + unit
    }
    str = `[${str}]`
    return str
}

const divideUrl = (url) => {
    let urlSearch = "";
    let urlHash = "";
    let urlPath = "";
    let hashIndex = url.indexOf("#/"); //hash 的位置,查询是否有哈希，有先分离，后面加回来
    let searchIndex = url.indexOf("?"); //参数的位置
    if (hashIndex !== -1) {
        //不存在hash
        if (hashIndex < searchIndex) {
            //哈希在前面，直接切
            urlSearch = url.split("#/")[1].split("?")[1];
            urlPath = url.split("#/")[0];
            urlHash = url.split("#/")[1].split("?")[0];
        } else {
            //在后面，需要先去掉哈希
            urlSearch = url.split("#/")[0].split("?")[1];
            urlPath = url.split("#/")[0].split("?")[0];
            urlHash = url.split("#/")[1].split("?")[1];
        }
    } else {
        urlSearch = url.split("?")[1];
        urlPath = url.split("?")[0];
    }
    return {
        urlHash,
        urlPath,
        urlSearch,
    };
};
